package com.resou.reader.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.resou.reader.R;

/**
 * Created by ${Garrett} on 2018/11/2.
 * Contact me krouky@outlook.com
 */
public class NetErrorFragment extends Fragment {

    private FragmentManager mFragmentManager;
    private OnClick mOnClick;

    public NetErrorFragment() {

    }

    @SuppressLint("ValidFragment")
    public NetErrorFragment(OnClick onClick) {
        this.mOnClick = onClick;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.net_error_layout, container, false);
        view.findViewById(R.id.net_error_container).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        view.findViewById(R.id.tvNet_error_text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClick != null) {
                    mOnClick.onClick();
                }
            }
        });
        return view;
    }

    public void show(int resId, @NonNull FragmentManager manager) {
        this.mFragmentManager = null;
        this.mFragmentManager = manager;
        manager.executePendingTransactions();
        if (!isAdded())
            manager.beginTransaction().add(resId, this).commitAllowingStateLoss();
    }

    public void dismiss() {
        if (mFragmentManager != null && isAdded() && !isDetached()) {
            mFragmentManager.beginTransaction().remove(this).commitAllowingStateLoss();
        }
    }

    public interface OnClick {
        void onClick();
    }
}
